#include "qnetworkgame.h"
#include "joingame.h"
#include <QDebug>

qnetworkgame::qnetworkgame(QWidget *parent) : QWidget(parent)
{
    setFixedSize(500,400);
    setWindowIcon(QIcon(":/res/R-C.jpg"));
    setWindowTitle("选择房间");

    tcpsocket=new QTcpSocket(this);

    game2=new netgame;
    connect(game2,&netgame::game_over,this,[=](){
        tcpsocket->write("r");
        this->tcpsocket->disconnectFromHost();

    });


    QPushButton* joinbtn=new QPushButton("加入房间",this);
    joinbtn->setFixedSize(200,60);
    joinbtn->setFont(QFont("SimHei",15,QFont::Bold));
    joinbtn->move(this->width()*0.5-joinbtn->width()*0.5,this->height()*0.2);
    connect(joinbtn,QPushButton::clicked,[=](){
        this->join_room();
    });

    QPushButton* backbtn=new QPushButton("退出游戏",this);
    backbtn->setFixedSize(200,60);
    backbtn->setFont(QFont("SimHei",15,QFont::Bold));
    backbtn->move(this->width()*0.5-backbtn->width()*0.5,this->height()*0.6);
    connect(backbtn,QPushButton::clicked,[=](){

        emit back_main();
    });

    connect(game2,&netgame::sent_black,this,[=](){
        int n=(this->game2->x*100+this->game2->y)*10;
        QByteArray send_data;
        send_data.setNum(n);
        tcpsocket->write(send_data);

    });
    connect(game2,&netgame::sent_white,this,[=](){
        int n=(this->game2->x*100+this->game2->y)*10+1;
        QByteArray send_data;
        send_data.setNum(n);
        tcpsocket->write(send_data);
    });


    connect(tcpsocket,&QTcpSocket::readyRead,this,[=](){
        QByteArray data=tcpsocket->readAll();
        if(data=="b"||data=="w")
        {
            for(int i=0;i<15;i++)
            {
                for(int j=0;j<15;j++)
                    this->game2->games->chess[i][j]=0;
            }
            this->game2->gamestatus=true;
            this->join1->wait->close();
            this->join1->close();
            this->hide();
            game2->show();
            if(data=="b")
            {
                QMessageBox::information(this,"连接成功！","您被选为黑色方先手", QMessageBox::Ok);
                this->game2->turn=true;
                this->game2->games->chess_status=true;
            }
            else
            {
                QMessageBox::information(this,"连接成功！","您被选为白色方后手", QMessageBox::Ok);
                this->game2->turn=false;
                this->game2->games->chess_status=false;
            }
        }
        else if(data=="r")
        {
            if(this->game2->gamestatus==true)
            {
                int res=QMessageBox::information(this,"连接断开","对方已退出游戏",QMessageBox::Ok);
                if(res==QMessageBox::Ok)
                {
                    tcpsocket->disconnectFromHost();
                    emit exit_game();
                }

            }

        }
        else
        {
            int n=data.toInt();
            int x=n/1000;
            int y=(n%1000)/10;
            int t=n%10;
            if(t==0)
            {
                this->game2->games->chess[x][y]=1;
                this->game2->update();
                this->game2->turn=!this->game2->turn;
                if(game2->winorn(x,y))
                {
                    int res=QMessageBox::information(this,"游戏结束","黑色方获胜！",QMessageBox::Ok);
                    if(res==QMessageBox::Ok)
                    {
                        QByteArray k;
                        k.setNum(1);
                        tcpsocket->write(k);
                        tcpsocket->disconnectFromHost();
                        emit exit_game();
                    }

                }
            }
            else if(t==1)
            {
                this->game2->games->chess[x][y]=-1;
                this->game2->update();
                this->game2->turn=!this->game2->turn;
                if(game2->winorn(x,y))
                {
                    int res=QMessageBox::information(this,"游戏结束","白色方获胜！",QMessageBox::Ok);
                    if(res==QMessageBox::Ok)
                    {
                        QByteArray k;
                        k.setNum(0);
                        tcpsocket->write(k);
                        tcpsocket->disconnectFromHost();
                        emit exit_game();
                    }

                }
            }
        }
    });

}


void qnetworkgame::join_room(void)
{
    join1=new joingame;

    connect(join1,&joingame::back,[=](){
        join1->hide();
        this->show();
        join1->wait->close();
        this->tcpsocket->disconnectFromHost();

    });


    connect(join1,&joingame::join_in,[=](){
       tcpsocket->connectToHost(QHostAddress(join1->IP),8888);
    });


    this->hide();
    join1->show();

}
